{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true,
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "# PaddlePaddle-Pandas"
   ]
  },
  {
   "cell_type": "markdown",
   "source": [
    "Pandas是python第三方库，提供高性能易用数据类型和分析工具\n",
    "\n",
    "Pandas基于Numpy实现，常与Numpy和Matplotlib一同使用"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "Pandas核心数据结构\n",
    "<img src=\"https://ai-studio-static-online.cdn.bcebos.com/a8c80653f39b479dab9f6867a638b64c405e79d6540c4307a22f43c4b0e228bc\">"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "<img src=\"https://ai-studio-static-online.cdn.bcebos.com/c8f06f423acc488fb391bca5dcf8f2b02d7444ef526f41599b6b430ae24659c1\">"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## Series"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "Series是一种类似于一维数组的对象，它由一维数组(各种numpy数据类型)\n",
    "\n",
    "以及一组与之相关的数据标签(索引)组成\n",
    "\n",
    "可理解为带标签的一维数组，可存储各类型数据"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.series.Series'>\n",
      "0    a\n",
      "1    b\n",
      "2    c\n",
      "3    d\n",
      "4    e\n",
      "dtype: object\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "s1=pd.Series(['a','b','c','d','e'])\n",
    "print(type(s1))\n",
    "print(s1)   #未指定索引"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "100    a\n",
      "200    b\n",
      "100    c\n",
      "400    d\n",
      "500    e\n",
      "dtype: object\n"
     ]
    }
   ],
   "source": [
    "#Series中可以使用index设置索引列表\n",
    "#与字典不同的是 Series中允许存在重复的索引\n",
    "s = pd.Series(['a','b','c','d','e'],index=[100,200,100,400,500])\n",
    "print(s)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.series.Series'>\n",
      "a    1\n",
      "b    2\n",
      "c    3\n",
      "dtype: int64\n"
     ]
    }
   ],
   "source": [
    "#Series可以用字典实例化\n",
    "dict1={'a':1,'b':2,'c':3}\n",
    "s2=pd.Series(dict1)\n",
    "print(type(s2))\n",
    "print(s2)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a    1\n",
      "b    2\n",
      "c    3\n",
      "dtype: int64\n"
     ]
    }
   ],
   "source": [
    "#可以通过Series中的values和index属性获取数组形式和索引对象\n",
    "print(s2)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 3]\n"
     ]
    }
   ],
   "source": [
    "print(s2.values)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Index(['a', 'b', 'c'], dtype='object')\n"
     ]
    }
   ],
   "source": [
    "print(s2.index)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "100    a\n",
      "200    b\n",
      "100    c\n",
      "400    d\n",
      "500    e\n",
      "dtype: object\n",
      "\n",
      "100    a\n",
      "100    c\n",
      "dtype: object\n",
      "400    d\n",
      "500    e\n",
      "dtype: object\n"
     ]
    }
   ],
   "source": [
    "#与普通numpy数组相比，可以通过索引的方式选取Series中的单个或者一组值\n",
    "print(s)\n",
    "\n",
    "print()\n",
    "print(s[100])\n",
    "print(s[[400, 500]])"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a    1\n",
      "b    2\n",
      "c    3\n",
      "d    4\n",
      "e    5\n",
      "dtype: int32\n",
      "a     2\n",
      "b     4\n",
      "c     6\n",
      "d     8\n",
      "e    10\n",
      "dtype: int32\n",
      "a     3\n",
      "b     6\n",
      "c     9\n",
      "d    12\n",
      "e    15\n",
      "dtype: int32\n"
     ]
    }
   ],
   "source": [
    "s = pd.Series(np.array([1,2,3,4,5]), index=['a', 'b', 'c', 'd', 'e'])\n",
    "print(s)\n",
    "\n",
    "#对应元素求和\n",
    "print(s+s)\n",
    "\n",
    "#对应元素乘\n",
    "print(s*3)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "Series中的一大特点：其会在算术运算中自动对齐不同索引的数据\n",
    "\n",
    "Series和多维数组的主要区别在于，Series之间的操作会自动基于标签对其数据。\n",
    "\n",
    "因此，不用顾及执行计算操作的Series是否具有相同的标签"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Ohio      35000\n",
      "Oregon    16000\n",
      "Texas     71000\n",
      "Utah       5000\n",
      "dtype: int64\n"
     ]
    }
   ],
   "source": [
    "obj1 = pd.Series({\"Ohio\": 35000, \"Oregon\": 16000, \"Texas\": 71000, \"Utah\": 5000})\n",
    "print(obj1)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "California        NaN\n",
      "Ohio          35000.0\n",
      "Oregon        16000.0\n",
      "Texas         71000.0\n",
      "dtype: float64\n"
     ]
    }
   ],
   "source": [
    "obj2 = pd.Series({\"California\": np.nan, \"Ohio\": 35000, \"Oregon\": 16000, \"Texas\": 71000})\n",
    "print(obj2)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "California         NaN\n",
      "Ohio           70000.0\n",
      "Oregon         32000.0\n",
      "Texas         142000.0\n",
      "Utah               NaN\n",
      "dtype: float64\n"
     ]
    }
   ],
   "source": [
    "print(obj1 + obj2)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "b    2\n",
      "c    3\n",
      "d    4\n",
      "e    5\n",
      "dtype: int32\n",
      "\n",
      "a    1\n",
      "b    2\n",
      "c    3\n",
      "d    4\n",
      "dtype: int32\n",
      "\n",
      "a    NaN\n",
      "b    4.0\n",
      "c    6.0\n",
      "d    8.0\n",
      "e    NaN\n",
      "dtype: float64\n"
     ]
    }
   ],
   "source": [
    "#切片\n",
    "s = pd.Series(np.array([1,2,3,4,5]), index=['a', 'b', 'c', 'd', 'e'])\n",
    "\n",
    "print(s[1:])\n",
    "print()\n",
    "print(s[:-1])\n",
    "print()\n",
    "print(s[1:] + s[:-1])"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## DataFrame"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "<img src=\"https://ai-studio-static-online.cdn.bcebos.com/c8f06f423acc488fb391bca5dcf8f2b02d7444ef526f41599b6b430ae24659c1\">"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "DataFrame是一个表格型的数据结构，类似于Excel或Sql表\n",
    "\n",
    "它含有一组有序的列，每列可以是不同的值类型\n",
    "\n",
    "DataFrame既有行索引也有列索引，它可以被看做由Series组成的字典（共用同一个索引）"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    state  year  pop\n",
      "0    Ohio  2000  1.5\n",
      "1    Ohio  2001  1.7\n",
      "2    Ohio  2002  3.6\n",
      "3  Nevada  2001  2.4\n",
      "4  Nevada  2002  2.9\n"
     ]
    }
   ],
   "source": [
    "#用多维数组字典、列表字典生成DataFrame\n",
    "data = {'state': ['Ohio', 'Ohio', 'Ohio', 'Nevada', 'Nevada'], 'year': [2000, 2001, 2002, 2001, 2002], 'pop': [1.5, 1.7, 3.6, 2.4, 2.9]}\n",
    "frame = pd.DataFrame(data)\n",
    "print(frame)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n"
     ]
    }
   ],
   "source": [
    "print(type(frame))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   year   state  pop\n",
      "0  2000    Ohio  1.5\n",
      "1  2001    Ohio  1.7\n",
      "2  2002    Ohio  3.6\n",
      "3  2001  Nevada  2.4\n",
      "4  2002  Nevada  2.9\n"
     ]
    }
   ],
   "source": [
    "#如果指定了列顺序，则DataFrame的列就会按照指定顺序进行排列\n",
    "frame1 = pd.DataFrame(data, columns=['year', 'state', 'pop'])\n",
    "print(frame1)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "outputs": [],
   "source": [
    "#和Series一样，如果传入的列在数据中找不到，就会产生NAN值\n",
    "frame2=pd.DataFrame(data,columns=['year','state','pop','debt'])"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   year   state  pop debt\n",
      "0  2000    Ohio  1.5  NaN\n",
      "1  2001    Ohio  1.7  NaN\n",
      "2  2002    Ohio  3.6  NaN\n",
      "3  2001  Nevada  2.4  NaN\n",
      "4  2002  Nevada  2.9  NaN\n"
     ]
    }
   ],
   "source": [
    "print(frame2)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   one  two\n",
      "a  1.0  1.0\n",
      "b  2.0  2.0\n",
      "c  3.0  3.0\n",
      "d  NaN  4.0\n"
     ]
    }
   ],
   "source": [
    "#我们也可以用Series字典或者字典生成DataFrame\n",
    "d = {'one': pd.Series([1., 2., 3.], index=['a', 'b', 'c']),\n",
    "     'two': pd.Series([1., 2., 3., 4.], index=['a', 'b', 'c', 'd'])}\n",
    "print(pd.DataFrame(d))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0      Ohio\n",
      "1      Ohio\n",
      "2      Ohio\n",
      "3    Nevada\n",
      "4    Nevada\n",
      "Name: state, dtype: object\n"
     ]
    }
   ],
   "source": [
    "#通过类似字典标记的方式或属性的方式\n",
    "#可以将DataFrame的列获取为一个Series，返回的Series拥有原DataFrame相同的索引\n",
    "print(frame2['state'])"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   year   state  pop  debt\n",
      "0  2000    Ohio  1.5  16.5\n",
      "1  2001    Ohio  1.7  16.5\n",
      "2  2002    Ohio  3.6  16.5\n",
      "3  2001  Nevada  2.4  16.5\n",
      "4  2002  Nevada  2.9  16.5\n"
     ]
    }
   ],
   "source": [
    "#列可以通过赋值的方式进行修改,例如，给那个空的“delt”列赋上一个标量值或一组值\n",
    "frame2['debt'] = 16.5\n",
    "print(frame2)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   year   state  pop  debt    new\n",
      "0  2000    Ohio  1.5  16.5  24.75\n",
      "1  2001    Ohio  1.7  16.5  28.05\n",
      "2  2002    Ohio  3.6  16.5  59.40\n",
      "3  2001  Nevada  2.4  16.5  39.60\n",
      "4  2002  Nevada  2.9  16.5  47.85\n"
     ]
    }
   ],
   "source": [
    "print(frame2)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   year   state  pop  debt    new\n",
      "0  2000    Ohio  1.5  16.5  24.75\n",
      "1  2001    Ohio  1.7  16.5  28.05\n",
      "2  2002    Ohio  3.6  16.5  59.40\n",
      "3  2001  Nevada  2.4  16.5  39.60\n",
      "4  2002  Nevada  2.9  16.5  47.85\n"
     ]
    }
   ],
   "source": [
    "frame2['new'] = frame2['debt' ]* frame2['pop']\n",
    "print(frame2)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "outputs": [],
   "source": [
    "frame2['debt']=np.arange(5.)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   year   state  pop  debt    new\n",
      "0  2000    Ohio  1.5   0.0  24.75\n",
      "1  2001    Ohio  1.7   1.0  28.05\n",
      "2  2002    Ohio  3.6   2.0  59.40\n",
      "3  2001  Nevada  2.4   3.0  39.60\n",
      "4  2002  Nevada  2.9   4.0  47.85\n"
     ]
    }
   ],
   "source": [
    "print(frame2)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}